package com.ustze.ocr.domain;

import cn.hutool.core.util.ReflectUtil;
import com.google.common.collect.Lists;
import com.ustze.ocr.domain.vo.DispatchDetailVo;
import com.ustze.ocr.domain.vo.DispatchNumberVo;
import com.ustze.ocr.domain.vo.DispatchPushVo;
import com.ustze.ocr.type.tencent.utils.TencentOcrUtils;
import com.ustze.ocr.type.tencent.vo.TencentResponse;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class TencentDispatchPdfBuild {


    public static void type(List<TencentResponse> list, DispatchDetailVo detailVo) {
        List<DispatchPushVo> tunnellingDispatch = Lists.newArrayList();
        List<DispatchPushVo> stopeDispatch = Lists.newArrayList();
        list.forEach(data -> {
            Long xsc = data.getColTl();
            Long xec = data.getColBr();

            for (int i = 0; i < ((xec - xsc) / 3); i++) {
                if (data.getText().contains("掘进")) {
                    tunnellingDispatch.add(DispatchPushVo.builder().build());
                } else if (data.getText().contains("回采")) {
                    stopeDispatch.add(DispatchPushVo.builder().build());
                }
            }
        });
        detailVo.setTunnellingDispatch(tunnellingDispatch);
        detailVo.setStopeDispatch(stopeDispatch);
    }


    /**
     * @param list
     * @param detailVo
     */
    public static void face(List<TencentResponse> list, DispatchDetailVo detailVo) {
        AtomicInteger count = new AtomicInteger(0);
        List<DispatchPushVo> tunnellingDispatch = detailVo.getTunnellingDispatch();
        List<DispatchPushVo> stopeDispatch = detailVo.getStopeDispatch();
        List<DispatchPushVo> listAll = Lists.newArrayList();
        listAll.addAll(tunnellingDispatch);
        listAll.addAll(stopeDispatch);

        listAll.forEach(data -> {
            TencentResponse aliOcrResponse = list.get(count.get());
            data.setWorkSurfaceName(aliOcrResponse.getText());
            Long xsc = aliOcrResponse.getColTl();
            Long xec = aliOcrResponse.getColBr();
            // 循环设置
            for (int i = 1; i <= (xec - xsc); i++) {
                ReflectUtil.setFieldValue(data, String.format("dispatch%s", i), DispatchNumberVo.builder().build());
            }
            count.incrementAndGet();
        });

    }

    /**
     * @param list
     * @param detailVo
     */
    public static void workNumber(List<TencentResponse> list, DispatchDetailVo detailVo) {
        AtomicInteger count = new AtomicInteger(0);
        List<DispatchPushVo> tunnellingDispatch = detailVo.getTunnellingDispatch();
        List<DispatchPushVo> stopeDispatch = detailVo.getStopeDispatch();
        List<DispatchPushVo> listAll = Lists.newArrayList();
        listAll.addAll(tunnellingDispatch);
        listAll.addAll(stopeDispatch);

        listAll.forEach(data -> {

            count.incrementAndGet();
        });
    }

    /**
     * @param list
     * @param detailVo
     */
    public static void workNumberDesc(List<TencentResponse> list, DispatchDetailVo detailVo) {
        AtomicInteger count = new AtomicInteger(0);
        List<DispatchPushVo> tunnellingDispatch = detailVo.getTunnellingDispatch();
        List<DispatchPushVo> stopeDispatch = detailVo.getStopeDispatch();
        List<DispatchPushVo> listAll = Lists.newArrayList();
        listAll.addAll(tunnellingDispatch);
        listAll.addAll(stopeDispatch);

        listAll.forEach(data -> {
            for (int i = 1; i <= 4; i++) {
                DispatchNumberVo fieldValue = (DispatchNumberVo)ReflectUtil.getFieldValue(data, String.format("dispatch%s", i));
                if (fieldValue != null && count.get() < list.size()) {
                    fieldValue.setCircMetre(Double.parseDouble(list.get(count.get()).getText().substring(0, 1)));
                    count.incrementAndGet();
                }
            }
        });
    }


    /**
     * @param list
     * @param detailVo
     */
    public static void remark(List<TencentResponse> list, DispatchDetailVo detailVo) {
        AtomicInteger count = new AtomicInteger(0);
        List<DispatchPushVo> tunnellingDispatch = detailVo.getTunnellingDispatch();
        List<DispatchPushVo> stopeDispatch = detailVo.getStopeDispatch();
        List<DispatchPushVo> listAll = Lists.newArrayList();
        listAll.addAll(tunnellingDispatch);
        listAll.addAll(stopeDispatch);


        listAll.forEach(data -> {
            for (int i = 1; i <= 4; i++) {
                DispatchNumberVo fieldValue = (DispatchNumberVo)ReflectUtil.getFieldValue(data, String.format("dispatch%s", i));
                if (fieldValue != null) {
                    fieldValue.setRemark(TencentOcrUtils.text(list.get(count.get()).getText()));
                    count.incrementAndGet();
                }
            }
        });
    }


}
